<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class RoleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
            case 'PATCH':
            case 'PUT':
                return [
                    'cn_name' => ['required', 'min:2', 'max:32', Rule::unique('roles')->ignore($this->route()->parameter('role'))],
                    'permission_id' => 'sometimes|nullable|array|exists:permissions,id'
                ];
            case 'POST':
                return [
                    'name' => 'required|min:2|max:32|unique:roles,name,' . $this->route()->parameter('role') . ',id',
                    'cn_name' => 'required|min:2|max:32|unique:roles,cn_name,' . $this->route()->parameter('role') . ',id',
                    'permission_id' => 'sometimes|nullable|array|exists:permissions,id'
                ];
        }
    }


    public function attributes()
    {
        return [
            'name' => '角色英文名',
            'cn_name' => '角色中文名',
            'permission' => '权限'
        ];
    }
}
